package org.visionlibrary.image.filters.effects;

import java.util.Random;

import javax.media.jai.TiledImage;

import org.visionlibrary.image.acquisition.ImageFactory;
import org.visionlibrary.image.model.AbstractFilter;


public class SimpleNoise extends AbstractFilter {
	protected double chance = 5; //wyrazone w procentach

	public SimpleNoise() {
		this(5);
	}

	public SimpleNoise(double chance) {
		this.chance = chance;
	}

	public TiledImage applyFilter(TiledImage src, TiledImage dest) {
		if (null == src)
			throw new NullPointerException("Source image is null.");

		if (null == dest)
			dest = (new ImageFactory()).createCompatibleImage(src);
		
		int maxX = src.getWidth();
		int maxY = src.getHeight();
		Random random = new Random();
		for (int x = 0; x < maxX; x++)
			for (int y = 0; y < maxY; y++) {
				double val = random.nextDouble() * 100;
				for (int ch = 0; ch < src.getNumBands(); ch++) {
					dest.setSample(x, y, ch, src.getSample(x, y, ch));
					if(val <= chance)
						dest.setSample(x, y, ch, 255);
				}
			}

		return dest;
	}
}
